package com.xhtt.modules.app.controller.photo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.modules.photo.entity.PhotoCustomer;
import com.xhtt.modules.photo.service.IPhotoCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 现场拍照客户列表 前端控制器
 * </p>
 *
 * @author xj
 * @since 2019-02-14
 */
@RestController
@RequestMapping("app/photo-customer")
@Api(value = "现场拍照-客户管理", tags = "现场拍照-客户管理")
public class PhotoCustomerController {

    @Autowired
    private IPhotoCustomerService photoCustomerService;

    /**
     * 获取现场拍照客户列表列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取现场拍照客户列表")
    public R list(@RequestParam(value = "name", required = false) String name) {
        return R.ok().put(Constant.DATA, photoCustomerService.queryList(name));
    }

    /**
     * 新增现场拍照客户列表
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增现场拍照客户列表")
    public R add(@RequestBody PhotoCustomer photoCustomer) {
        int nameExist = photoCustomerService.count(new LambdaQueryWrapper<PhotoCustomer>().eq(PhotoCustomer::getName, photoCustomer.getName()));
        if (nameExist > 0) {
            throw new RRException(RRExceptionEnum.CUSTOMER_NAME_EXIST);
        }
        photoCustomerService.save(photoCustomer);
        return R.ok();
    }

    /**
     * 删除现场拍照客户列表
     */
    @PostMapping(value = "/delete/{id}")
    @ApiOperation("删除现场拍照客户列表")
    public R delete(@PathVariable("id") Integer id) {
        photoCustomerService.delete(id);
        return R.ok();
    }

    /**
     * 修改现场拍照客户列表
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改现场拍照客户列表")
    public R update(@RequestBody PhotoCustomer photoCustomer) {
        int nameExist = photoCustomerService
                .count(new LambdaQueryWrapper<PhotoCustomer>()
                        .eq(PhotoCustomer::getName, photoCustomer.getName())
                        .ne(PhotoCustomer::getId, photoCustomer.getId()));
        if (nameExist > 0) {
            throw new RRException(RRExceptionEnum.CUSTOMER_NAME_EXIST);
        }
        photoCustomerService.updateById(photoCustomer);
        return R.ok();
    }

    /**
     * 获取现场拍照客户列表
     */
    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取现场拍照客户列表")
    public R get(@PathVariable("id") Long id) {
        return R.ok().put(Constant.DATA, photoCustomerService.getById(id));
    }

}
